class SendToFriendsGenerator < Rails::Generator::Base
  def manifest
    record do |m|
      # create directories that might not exist
      m.directory File.join('app', 'views', 'send_to_friends')
      m.directory File.join('app', 'views', 'friend_mailer')
      # controller
      m.file 'send_to_friends_controller.rb', File.join('app', 'controllers', 'send_to_friends_controller.rb')
      # stf views
      %w(_errors.rhtml errors.rxml create.rhtml index.rhtml).each do |f|
        m.file f, File.join('app', 'views', 'send_to_friends', f)
      end
      # models
      %w(friend_mailer.rb recipient.rb sender.rb).each do |f|
        m.file f, File.join('app', 'models', f)
      end
      # email file
      m.file 'message.rhtml', File.join('app', 'views', 'friend_mailer', 'message.rhtml')

      add_route
      m.readme "INSTRUCTIONS"
    
    end
  end
  
  def add_route
    # now add a route
    route_definition  = "map.resources :send_to_friends"
    routes_file       = "#{RAILS_ROOT}/config/routes.rb"
    begin
      lines = IO.readlines(routes_file)
      # add the route on line 2
      File.open(routes_file, 'w') do |f|
        lines.insert(1, "  #{route_definition}\n")
        f.write lines.join
      end
    rescue => e
      puts "Could not add route to your config/routes.rb : #{e} \n\n"
      puts "Please add '#{route_definition}' to your routes file"
    end
    
  end
  
end

